<?php
session_start();
if(isset($_SESSION['admin_id'])) {
    $mes ="";
    if (isset($_SESSION['mes'])) {
        $mes=$_SESSION['mes'];
        $_SESSION['mes']="";
    }
    include "../include/dbFunctions.php";
    include "../include/formFunctions.php";
    include "../include/menu.php";

//GET program ID from URL just clicked
    if (!isset($_GET['id'])) {
        header("Location programManage.php");
    }else {
        $id = $_GET['id'];

        $_SESSION['program_id'] = $id; //Make $id become GLOBAL for passing to next page for UPDATE or DELETE
//Get data from program table
        $query="select * from program p, program_education pe,education e WHERE p.program_id = $id
AND p.program_id=pe.program_id
AND e.education_id=pe.education_id";

        $program_result=executeSelectQuery($query);

        $id = $program_result[0]['program_id'];
        $name = $program_result[0]['program_name'];
        $description = $program_result[0]['program_description'];
        $timelesson = $program_result[0]['program_lesson_duration'];
        $support = $program_result[0]['program_support'];
        $status = $program_result[0]['program_status'];
        $education = $program_result[0]['education_name'];
//Data from preferred educational level
        $msg = "";
        $query3 = "SELECT e.education_name
                FROM education e, program_education pe,program p
                WHERE e.education_id = pe.education_id
                AND p.program_id=pe.program_id
                AND p.program_id = $id";
        $education_array = executeSelectQuery($query3);
        if ($education_array==null) {
            $msg = "No preference";
        }
//Get data from event table
        $query1="select * from event WHERE program_id = $id AND event_status!='cancelled'";
        $event_result= executeSelectQuery ($query1);



        ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http//www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http//www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>JA Scheduling</title>

        <script type="text/javascript" language="javascript" src="../media/js/jquery/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" language="javascript" src="../media/js/jquery/dataTable.js"></script>

        <!--  CSS script -->
        <link rel="stylesheet" href="../media/css/screen.css" type="text/css" media="screen" title="default" />
        <link type="text/css" href="../media/css/dataTable.css" rel="stylesheet" />

        <!-- Manage table script -->
        <script type="text/javascript" charset="utf-8">
            $(document).ready(function() {
                $('#admintable').dataTable( {
                    "sPaginationType" "full_numbers"
                } );
            } );
        </script>
    </head>
    <body>
        <!-- DON'T TOUCH! Start page-top-outer -->
                <?php
                top_outer();
                ?>
        <!-- End page-top-outer -->

        <div class="clear">&nbsp;</div>

        <!--  start nav-outer-repeat................................................................................................. START -->
        <div class="nav-outer-repeat">
            <!--  start nav-outer -->
            <div class="nav-outer">

                <!-- start nav-right -->
                        <?php
                        nav_right();
                        ?>
                <!-- end nav-right -->


                <!--  start nav -->
                <div class="nav">
                    <div class="table">

                                <?php
                                navigation_program();
                                ?>

                        <div class="clear"></div>
                    </div>
                    <div class="clear"></div>
                </div>
                <!--  start nav -->

            </div>
            <div class="clear"></div>
            <!--  start nav-outer -->
        </div>
        <!--  start nav-outer-repeat................................................... END -->

        <div class="clear"></div>

        <!-- start content-outer ........................................................................................................................START -->
        <div id="content-outer">
            <!-- start content -->
            <div id="content">

                <!--  start page-heading --> <!--lyedit-->
                <div id="page-heading">
                    <h1>View Program</h1>
                </div>
                <!-- end page-heading -->

                <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
                    <tr>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
                        <th class="topleft"></th>
                        <td id="tbl-border-top">&nbsp;</td>
                        <th class="topright"></th>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
                    </tr>
                    <tr>
                        <td id="tbl-border-left"></td>
                        <td>
                            <!--  start content-table-inner ...................................................................... START -->
                            <div id="content-table-inner">
                                <h2></h2>
                                <!--  start table-content  -->
                                <div id="table-content">
                                    <!--  Can put table start from here within this div-->

                                    <div id="details_content">
                                        <font color="red"><b><?php echo $mes?></b></font>
                                        <table border="0" cellpadding="0" cellspacing="0"  id="id-form">
                                            <tr>
                                                <th valign="top">Program name</th>
                                                <td class="program_name" id="program_name"><?php echo $name ; ?></td>
                                                <td></td>
                                            </tr>

                                            <tr>
                                                <th valign="top">Program description</th>
                                                <td><table width="500">
                                                        <tr><td><?php echo $description; ?></td></tr>
                                                    </table>
                                                </td>
                                            </tr>
                                            <tr>
                                                <th valign="top">Education Level</th>
                                                <td>
                                                            <?php
                                                            echo $msg;
                                                            for ($i=0;$i<count($education_array);$i++) {
                                                                $education = ucwords($education_array[$i]['education_name']);
                                                                echo "- ".$education."<br/>";
                                                            }
                                                            ?></td>
                                                <td></td>
                                            </tr>
                                            <tr>
                                                <th valign="top">Duration (hours) </th>
                                                <td class="program_lesson_duration" id="program_lesson_duration"><?php echo $timelesson; ?></td>
                                                <td></td>
                                            </tr>
                                           <tr>
                                                <th valign="top">Support requirement</th>
                                                <td><table width="500">
                                                        <tr><td><?php echo $support; ?></td></tr>
                                                    </table>
                                                </td>
                                            </tr>
                                            <tr>
                                                <th valign="top">Status</th>
                                                <td class="program_status" id="program_status"><?php echo $status; ?></td>
                                                <td></td>
                                            </tr>
                                        </table>
                                        <table>
                                            <tr>
                                                <th>
                                                    <td>
                                                        <input type="button" class="form-back" value="&nbsp" onClick="javascripthistory.go(-1)"/>
                                                    </td>
                                                    <td>
                                                        <form method="POST" action="programEdit.php" id="submitform">
                                                            <input type="hidden" name="id" value='<?php echo $id;?>'></input>
                                                            <input type="Submit" name="Edit" class="form-edit" value="&nbsp"/>
                                                        </form>
                                                    </td>
                                                    <td>
                                                        <form method="GET" id="submitform" action="programDelete.php" >
                                                            <input type="hidden" name="id" value='<?php echo $id;?>'></input>
                                                            <input type="submit" class="form-delete" onClick="if (confirm('Are you sure you want to delete?')) commentDelete(1); return false" value="&nbsp"/>
                                                        </form>
                                                    </td>
                                                </th>
                                            </tr>
                                        </table>
                                    </div><br/><br/>
                                    <h3><?php echo $name; ?>'s Events</h3>
                                    <table cellpadding="0" cellspacing="0" border="0" class="display" id="admintable">
                                        <thead>
                                            <tr>
                                                <th>No.</th>
                                                <th>Event</th>                                                
                                                <th>Status</th>

                                            </tr>
                                        </thead>
                                        <tbody>
                                                    <?php
                                                    for ($i=0;$i<count($event_result);$i++) {
                                                        $no = $i+1;
                                                        $event_id = $event_result[$i]['event_id'];
                                                        $event_name = $event_result[$i]['event_name'];
                                                        $event_status = getOpenStatus($event_result[$i]['event_status']);

                                                        ?>
                                            <tr>
                                                <td><?php echo $no; ?></td>
                                                <td><a href='../event/eventView.php?id=<?php echo $event_id?>'><?php echo $event_name?></a></td>
                                                <td><?php echo $event_status; ?></td>
                                            </tr>
                                                        <?php
                                                    }
                                                    ?>
                                        </tbody>
                                    </table>

                                    <!--  end table-content here -->
                                </div>
                                <div class="clear"></div>
                            </div>
                            <!--  end content-table-inner ............................................END  -->
                        </td>
                        <td id="tbl-border-right"></td>
                    </tr>
                    <tr>
                        <th class="sized bottomleft"></th>
                        <td id="tbl-border-bottom">&nbsp;</td>
                        <th class="sized bottomright"></th>
                    </tr>
                </table>
                <div class="clear">&nbsp;</div>

            </div>
            <!--  end content -->
            <div class="clear">&nbsp;</div>
        </div>
        <!--  end content-outer........................................................END -->
                <?php
                footer();
                ?>

    </body>
</html>
        <?php
    }
}else {
    header("Location ../include/login.html");
}
?>